﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Compiler
{
    public static class Builder
    {
        public enum BuildMethod
        {
            LR0, SLR,
            LR1, LALR
        }

        public static ParseTable Build(Grammar grammar, BuildMethod buildMethod, string printGraphPath)
        {
            if (buildMethod == BuildMethod.LR0 || buildMethod == BuildMethod.SLR)
            {
                LR0Builder lr0 = new LR0Builder(grammar, buildMethod);
                lr0.BuildStatemachine();
                return lr0.BuildTable();
            }
            else if (buildMethod == BuildMethod.LR1 || buildMethod == BuildMethod.LALR)
            {
                LR1Builder lr1 = new LR1Builder(grammar, buildMethod);
                lr1.BuildStatemachine();

                ////dot -Tpng hurr.gv -o state2.png
                if (!string.IsNullOrEmpty(printGraphPath))
                {

                    string vis = lr1.VisualGraph();
                    File.WriteAllText(printGraphPath, vis);
                }

                return lr1.BuildTable();
            }

            throw new NotImplementedException();
        }
    }
}
